package com.dxysun.springboot.advice;

import java.io.IOException;
import java.lang.reflect.Type;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

//@Component
@ControllerAdvice
public class MyRequestBodyAdvice implements RequestBodyAdvice
{
	private static final Log logger = LogFactory.getLog(MyRequestBodyAdvice.class);

	@Override
	public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass)
	{
		return true;
	}

	@Override
	public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter,
			Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException
	{
		logger.info("MyRequestBodyAdvice beforeBodyRead");
		return httpInputMessage;
	}

	@Override
	public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type,
			Class<? extends HttpMessageConverter<?>> aClass)
	{
		logger.info("MyRequestBodyAdvice afterBodyRead");
		return o;
	}

	@Override
	public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter,
			Type type, Class<? extends HttpMessageConverter<?>> aClass)
	{
		logger.info("MyRequestBodyAdvice handleEmptyBody");
		return o;
	}
}
